<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/3/12
 * Time: 9:54
 */

namespace App\Http\Controllers\Api\V1;


use App\Http\Resources\DocCollection;
use App\Http\Resources\UserCollection;
use App\Models\Doc;
use App\Models\DocArticle;
use App\Models\Liveroom;
use App\User;

class FollowController extends BaseController
{
    /**
     * @throws \App\Exceptions\ApiException
     */
    public function follow_user()
    {
        $id = $this->getId();
        $user = $this->getUser();
        $obj = User::query()->findOrFail($id);
        if ($obj->isFollowedBy($user)) {
            $user->unfollow($obj);
            return $this->message('已取消关注', 'off');
        } else {
            $user->follow($obj);
            return $this->message('已关注', 'on');
        }
    }

    /**
     * 关注、取消关注直播间
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function follow_liveroom()
    {
        $id = $this->getId();
        $user = $this->getUser();
        $liveroom = Liveroom::query()->findOrFail($id);
        if (Liveroom::judgeFollow($liveroom, $user)) {
            $user->unfollow($liveroom);
            return $this->message('已取消关注', 'off');
        } else {
            $user->follow($liveroom);
            return $this->message('已关注', 'on');
        }
    }

    /**
     * 预约专栏
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function subscribe_channel()
    {
        $id = $this->getId();
        $user = $this->getUser();
        $obj = Channel::query()->findOrFail($id);
        if ($obj->isSubscribedBy($user)) {
            $user->unsubscribe($obj);
            return $this->message('已取消预约', 'off');
        } else {
            $user->subscribe($obj);
            return $this->message('预约成功', 'on');
        }
    }

    /**
     * 收藏课程
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function favorite_lecture()
    {
        $id = $this->getId();
        $user = $this->getUser();
        $obj = Lecture::query()->findOrFail($id);
        if ($obj->isFavoritedBy($user)) {
            $user->unfavorite($obj);
            return $this->message('已取消收藏', 'off');
        } else {
            $user->favorite($obj);
            return $this->message('收藏成功', 'on');
        }
    }

    /**
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function subscribe_doc()
    {
        $id = $this->getId();
        $user = $this->getUser();
        $obj = Doc::query()->findOrFail($id);
        if ($obj->isSubscribedBy($user)) {
            $user->unsubscribe($obj);
            return $this->message('已取消收藏', 'off');
        } else {
            $user->subscribe($obj);
            return $this->message('收藏成功', 'on');
        }
    }

    /**
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function like_article()
    {
        $id = $this->getId();
        $user = $this->getUser();
        $obj = DocArticle::query()->findOrFail($id);
        if ($obj->isLikedBy($user)) {
            $user->unlike($obj);
            return $this->message('已取消点赞', 'off');
        } else {
            $user->like($obj);
            return $this->message('已点赞', 'on');
        }
    }


    public function subscribe_doc_list()
    {
        $user = $this->getUser();
        $obj = $user->subscriptions(\App\Models\Doc::class)->orderByDesc('followables.created_at')->with([
            'category',
            'tags'
        ]);
        $page = $obj->paginate();
        $data = DocCollection::make($page)->show(['id', 'cover', 'title', 'tags', 'category', 'category_name','mode']);
        return $this->success($data);
    }

    public function follows()
    {
        $user = $this->getUser();
        $orm = $user->followings()->orderByDesc('followables.created_at');
        $page = $orm->paginate();
        $data = UserCollection::make($page)->show(['id', 'name', 'avatar', 'intro', 'is_follow']);
        return $this->success($data);
    }

    public function fans()
    {
        $user = $this->getUser();
        $orm = $user->followers()->orderByDesc('followables.created_at');
        $page = $orm->paginate();
        $data = UserCollection::make($page)->show(['id', 'name', 'avatar', 'intro', 'gender', 'province', 'city']);
        return $this->success($data);
    }
}